package org.example.service.impl;

import org.example.domain.SysMenuRole;
import org.example.domain.SysRole;
import org.example.dto.RoleDto;
import org.example.mapper.SysMenuRoleMapper;
import org.example.mapper.SysRoleMapper;
import org.example.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuRoleMapper sysMenuRoleMapper;

    @Override
    public List<SysRole> rolesList() {
        return sysRoleMapper.rolesList();
    }

    @Override
    public List<SysRole> roleList(RoleDto roleDto) {
        return sysRoleMapper.roleList(roleDto);
    }

    @Override
    public List<SysRole> list() {
        return sysRoleMapper.list();
    }

    @Override
    public Boolean roleDels(List<Long> id) {
        return sysRoleMapper.roleDels(id);
    }

    @Override
    @Transactional
    public void save(SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRoleMapper.save(sysRole);

        Long[] mids = sysRole.getMids();
        if(mids.length>0 && mids!=null){
            for (Long mid : mids) {
                SysMenuRole sysMenuRole = new SysMenuRole();
                sysMenuRole.setMenuId(mid);
                sysMenuRole.setRoleId(sysRole.getRoleId());
                sysMenuRoleMapper.save(sysMenuRole);
            }
        }


    }

    @Override
    public SysRole listById(Long id) {
        return sysRoleMapper.listById(id);
    }

    @Override
    public void update(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
        sysMenuRoleMapper.delsById(sysRole.getRoleId());
        Long[] mids = sysRole.getMids();
        if(mids.length > 0 && mids!= null){
            for (Long mid : mids) {
                SysMenuRole sysMenuRole = new SysMenuRole();
                sysMenuRole.setMenuId(mid);
                sysMenuRole.setRoleId(sysRole.getRoleId());
                sysMenuRoleMapper.save(sysMenuRole);
            }
        }
    }

    @Override
    public List<SysRole> getrole() {
        return sysRoleMapper.getrole();
    }

    @Override
    public void insertBath(List<SysRole> cacheList) {
        sysRoleMapper.insertBath(cacheList);
    }
}
